package com.fsk.androidclient;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import org.jeromq.*;
import org.jeromq.ZContext;
import org.jeromq.ZMQ;
import org.jeromq.ZMQ.Msg;
import android.os.AsyncTask;
import android.os.Handler;
import android.widget.Toast;
import android.util.Log;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import fsk.msg.FskMsgMessages;
import java.nio.ByteBuffer;
import android.widget.EditText;



public class androidClient extends Activity
{
  private Button button_;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button_ = (Button)findViewById(R.id.button);
         button_.setOnClickListener(new OnClickListener() {
                 public void onClick (View v){
                   final String MethodName = "OnClickListener::onClick";
                   go();
                 }
             });

    }

    private void go()
    {
EditText entry = (EditText)findViewById(R.id.brokerEntry);
Log.d("**",""+entry.getText());
      Toast.makeText(this, "sending msg", Toast.LENGTH_SHORT).show();
      ZeroMqMsgTask t=new ZeroMqMsgTask(null);
      final String uri=entry.getText().toString();
      t.execute(uri);
      Toast.makeText(this, "done sending msg", Toast.LENGTH_SHORT).show();
    }

    private class ZeroMqMsgTask extends AsyncTask<String, Void, String> {
        private final Handler uiThreadHandler;
        public ZeroMqMsgTask(Handler uiThreadHandler)
        {
                this.uiThreadHandler = uiThreadHandler;
        }
        protected String doInBackground(String...params)
        {
                //String url="tcp://192.168.0.16:6001";
                String url=params[0];
                ZMQ.Context context=ZMQ.context(1);
                ZMQ.Socket publisher=context.socket(ZMQ.PUB);
                publisher.connect(url);

                String topic=new String("fsk.msg.Message01");
                final int N = 500;
                for(int i=0; i<N; ++i)
                {
                  Msg mTopic = new Msg(topic);
                  boolean b1=publisher.send(mTopic,ZMQ.SNDMORE);
                  fsk.msg.FskMsgMessages.Message01 msg01=fsk.msg.FskMsgMessages.Message01.newBuilder().setVal01(i).build();
                  {
                    ByteBuffer B = ByteBuffer.allocateDirect(200);
                    byte[] bytes=msg01.toByteArray();
                    for(int c=0; c<bytes.length; ++c)
                    {
                      B.put(bytes[c]);
                    }

                    Msg mymsg = new Msg(B);
                    boolean b2=publisher.send(mymsg,0);
                  }
                  Log.d("here:", url + "; iteration " + i);
                  try 
                  {
                    Thread.sleep(10);
                  } catch (InterruptedException e)
                  {
                    e.printStackTrace();
                  }
                }
                publisher.close();
                return "";
        }

        protected void onPostExecute(String result)
        {

        }
    };

}
